package appcus

import (
	"counter-help/admin/server/global"
	"counter-help/admin/server/model/app/request"
	"counter-help/admin/server/model/common/response"
	"counter-help/admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type CusOrderEvaluateApi struct{}

func (e *CusOrderEvaluateApi) CreateOrderEvaluate(c *gin.Context) {

	// 执行收藏操作
	userID := utils.GetUserID(c)

	// 绑定 JSON 参数
	var OrderEvqReq request.OrderEvaReq
	if err := c.ShouldBindJSON(&OrderEvqReq); err != nil {
		response.FailWithMessage("参数无效", c)
		return
	}
	err := cusOrderEvaluateService.CreateCusOrderEvaluate(int(userID), &OrderEvqReq)
	if err != nil {
		global.GVA_LOG.Error("收藏失败!", zap.Error(err))
		response.FailWithMessage("收藏失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("评价成功", c)
}

// DeleteOrderEvaluate 删除评价
func (e *CusOrderEvaluateApi) DeleteOrderEvaluate(c *gin.Context) {
	userID := utils.GetUserID(c)

	var orderInfo request.DeleteOrderEvaReq
	err := c.ShouldBindQuery(&orderInfo)
	if err != nil {
		response.FailWithMessage("参数解析失败: "+err.Error(), c)
		return
	}

	err = cusOrderEvaluateService.DeleteCusOrderEvaluate(int(userID), &orderInfo)
	if err != nil {
		response.FailWithMessage("删除失败: "+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// UpdateOrderEvaluate 更新所有评价
func (e *CusOrderEvaluateApi) UpdateOrderEvaluate(c *gin.Context) {
	userID := utils.GetUserID(c)

	var updateReq request.UpdateOrderEvaReq
	if err := c.ShouldBindJSON(&updateReq); err != nil {
		response.FailWithMessage("参数无效", c)
		return
	}

	err := cusOrderEvaluateService.UpdateCusOrderEvaluate(int(userID), &updateReq)
	if err != nil {
		response.FailWithMessage("修改失败: "+err.Error(), c)
		return
	}
	response.OkWithMessage("修改成功", c)
}

// GetOrderEvaluates 查看订单的所有评价
func (e *CusOrderEvaluateApi) GetOrderEvaluates(c *gin.Context) {
	var orderInfo request.GetOrderEvaReq
	err := c.ShouldBindQuery(&orderInfo)
	if err != nil {
		response.FailWithMessage("参数解析失败: "+err.Error(), c)
		return
	}
	evaluates, err := cusOrderEvaluateService.GetOrderEvaluateByOrderID(orderInfo.OrderId)
	if err != nil {
		response.FailWithMessage("查询失败: "+err.Error(), c)
		return
	}
	response.OkWithData(evaluates, c)
}

// GetAllOrderEvaluates 查看订单的所有评价
func (e *CusOrderEvaluateApi) GetAllOrderEvaluates(c *gin.Context) {
	evaluates, err := cusOrderEvaluateService.GetUnionAllOrderEvaluate()
	if err != nil {
		response.FailWithMessage("查询失败: "+err.Error(), c)
		return
	}
	response.OkWithData(evaluates, c)
}
